package com.ln.adoms.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.ln.adoms.dao.RegionDao;
import com.ln.adoms.entity.Region;
import com.ln.adoms.entity.bean.RegionBean;
import com.ln.adoms.service.RegionService;
import com.ln.core.orm.PageData;
import com.ln.core.utils.WebUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/adoms/region")
public class RegionAction {
	@Resource
	private RegionService regionService;
	@Resource
	private RegionDao regionDao;
	
	@RequestMapping(value="/query/{parentId}", method=RequestMethod.GET)
	public @ResponseBody List<RegionBean> query(@PathVariable Integer parentId, Model model) {
		List<Region> regionList = null;

		regionList = regionDao.getCityListByParentId(parentId);
		List<RegionBean> regionBeanList = new ArrayList<RegionBean>();
		for (Region region : regionList) {
			RegionBean regionBean = new RegionBean();
			regionBean.setId(region.getId());
			regionBean.setParentId(region.getParent().getId());
			regionBean.setLocationName(region.getName());
			//把四川放到第一个
			if (parentId == 0 && region.getId() == 4771) {
				regionBeanList.add(0, regionBean);
			} else if (parentId == 4771 && region.getId() == 7204) { //把成都放在第一个
				regionBeanList.add(0, regionBean);
			} else {
				regionBeanList.add(regionBean);
			}
		}
		return regionBeanList;
	}

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<Region> pageData = new PageData<Region>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = regionService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		return "/adoms/region/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		Region region = regionService.find(id);
		model.addAttribute(region);
		return "/adoms/region/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Region region) {
		if (region.isNew()) {
			regionService.save(region);
		} else {
			regionService.update(region);
		}

		return "redirect:/adoms/region/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		regionService.delete(id);
		return "redirect:/adoms/region/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		regionService.visible(id);
		return "redirect:/adoms/region/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		regionService.unVisible(id);
		return "redirect:/adoms/region/list";// 重定向
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
